package common.web.servlet;

import javax.servlet.*;
import javax.servlet.http.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import util.DebugUtil;

import common.service.CacheService;
import common.service.ICacheService;
import common.service.ServiceFactory;

import java.io.*;


public class InitServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final String CONTENT_TYPE = "text/html; charset=UTF-8";
	private static Log log = LogFactory.getLog(InitServlet.class);
	
	private ICacheService cacheService;

	// Initialize global variables
	public void init() throws ServletException {
		System.setProperty("WORKDIR", getServletContext().getRealPath("/"));
		cacheService = (ICacheService) ServiceFactory.getService(new CacheService());
		
		//初始化所有缓存数据
		DebugUtil.print("初始化开始----------------------");
		try {
			cacheService.initAllCacheData();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.info("初始化出错：",e);
		}	
		DebugUtil.print("初始化结束----------------------");
	}

	// Process the HTTP Get request
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType(CONTENT_TYPE);
		response.setHeader("Pragma", "no-cache"); // HTTP1.0
		response.setHeader("Cache-Control", "no-store"); // HTTP1.1
		response.setDateHeader("Expires", 0);
	}

	// Process the HTTP Post request
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	// Clean up resources
	public void destroy() {
		//清空所有缓存数据
		cacheService.clearAll();
	}

	

}
